<?php
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
return;
}

require_once('lib/db_connection.php');
$company_id = $_SESSION['company_id'];
$p1 = $_GET['p1'];
$p2 = $_GET['p2'];
$p3 = $_GET['p3'];

if($p2=='LIKE')
$passed_where = $p1." ".$p2." '%".$p3."%'";
if($p2!='LIKE')
$passed_where = $p1." ".$p2." '".$p3."'";

	$filename = "Fuel_Report".date('Y-m-d');
    // Send Header
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$filename.xls "); // แล้วนี่ก็ชื่อไฟล์
    header("Content-Transfer-Encoding: binary ");

	
	
   // Query Database
    if($p3==''||$p3==null)
	{
	$query_fuel = "SELECT * FROM veh_fuel_tbl
	WHERE company_id='".$company_id."'";
	}

	if($p3!=''||$p3!=null)
	{
	$query_fuel = "SELECT * FROM veh_fuel_tbl
	WHERE company_id='".$company_id."'
	AND ".$passed_where;	
	}
	$result_fuel = mysql_query($query_fuel)or die(mysql_error());
   
    // XLS Data Cell

                xlsBOF();
                /*xlsWriteLabel(1,0,"Student Register $semester/$year");
                xlsWriteLabel(2,0,"COURSENO : ");
                xlsWriteLabel(2,1,"$courseid");
                xlsWriteLabel(3,0,"TITLE : ");
                xlsWriteLabel(3,1,"$title");
                xlsWriteLabel(4,0,"SETION : ");
                xlsWriteLabel(4,1,"$sec");
                xlsWriteLabel(6,0,"Plate");
                xlsWriteLabel(6,1,"Model");
                xlsWriteLabel(6,2,"Manufacturer");
                xlsWriteLabel(6,3,"Track By");
                xlsWriteLabel(6,4,"Odometer/Hrs");
                $xlsRow = 7;*/
                xlsWriteLabel(0,0,"Date");
                xlsWriteLabel(0,1,"Driver");
                xlsWriteLabel(0,2,"Brand");
                xlsWriteLabel(0,3,"Type");
                xlsWriteLabel(0,4,"Liters");
                xlsWriteLabel(0,5,"Price/Liter");
                xlsWriteLabel(0,6,"Total");
                xlsWriteLabel(0,7,"Km/Hrs");
                $xlsRow = 1;
                while($row=mysql_fetch_array($result_fuel)) {
                    ++$i;
                    
            	//TRACKING
				$tracking = $row['track_by'];
				if($tracking=='distance')
				$curr_od = $row['miles'].'Km';
				if($tracking=='hours')
				$curr_od = $row['miles'].'Hrs';
                
				//xlsWriteNumber
				    
                xlsWriteLabel($xlsRow,0,$row['date']);
                xlsWriteLabel($xlsRow,1,$row['driver']);
                xlsWriteLabel($xlsRow,2,$row['brand']);
                xlsWriteLabel($xlsRow,3,$row['type']);
                xlsWriteLabel($xlsRow,4,$row['gallons']);
                xlsWriteLabel($xlsRow,5,$row['price']);
                xlsWriteLabel($xlsRow,6,$row['tot_cost']);
                xlsWriteLabel($xlsRow,7,$curr_od);
                $xlsRow++;
				}                    
                xlsEOF();
                exit();
?>